package cn.gailvlun.gll.db;

import java.util.Date;

import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.apartment.room.Room;
import cn.gailvlun.gll.net.base.Profile;
import cn.gailvlun.gll.net.cross.CrossQuestionInfo;
import cn.gailvlun.gll.net.cross.CrossRoomInfo;
import cn.gailvlun.gll.net.emotion.Emotion;
import io.realm.DynamicRealm;
import io.realm.FieldAttribute;
import io.realm.RealmMigration;
import io.realm.RealmSchema;

public class NetMigration implements RealmMigration {

    @Override
    public int hashCode() {
        return 4;
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof NetMigration;
    }


    @Override
    public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema schema = realm.getSchema();

        if (oldVersion == 1) {
            schema.create(CrossQuestionInfo.class.getSimpleName())
                    .addField("id", int.class, FieldAttribute.PRIMARY_KEY)
                    .addRealmObjectField("owner", schema.get(Profile.class.getSimpleName()))
                    .addField("question", String.class)
                    .addField("answer", String.class)
                    .addField("otherAnswer", String.class);

            schema.create(CrossRoomInfo.class.getSimpleName())
                    .addField("id", int.class, FieldAttribute.PRIMARY_KEY)
                    .addField("beforeOpenTime", int.class)
                    .addField("riddleTime", int.class)
                    .addField("riddleCountDownTime", int.class)
                    .addField("riddleAnswerTime", int.class)
                    .addField("punishTime", int.class)
                    .addField("punishCountDownTime", int.class)
                    .addField("halfTime", int.class)
                    .addField("commentingTime", int.class)
                    .addRealmObjectField("first", schema.get(CrossQuestionInfo.class.getSimpleName()))
                    .addRealmObjectField("second", schema.get(CrossQuestionInfo.class.getSimpleName()));

            oldVersion++;
        }

        if (oldVersion == 2) {
            schema.get(Member.class.getSimpleName())
                    .addField("block", boolean.class);

            oldVersion++;
        }

        if (oldVersion == 3) {
            schema.get(CrossQuestionInfo.class.getSimpleName())
                    .addField("question_ref", String.class);
            oldVersion++;
        }

        if (oldVersion == 4) {
            schema.get(Room.class.getSimpleName())
                    .addField("apply_datetime", Date.class);
            schema.get(Profile.class.getSimpleName())
                    .addField("is_profile_public", Boolean.class);
            schema.get(Emotion.class.getSimpleName())
                    .addField("anony", String.class);
            oldVersion++;
        }
    }
}
